﻿using System;
using System.Collections.Generic;
using System.Text;
using pos.entity;
using System.Collections;

namespace pos.entity
{
    class StockStorageInfo
    {
        double profit;
        double cost;
        int storageState;

        public int StorageState
        {
            get { return storageState; }
            set { storageState = value; }
        }
        public double Profit
        {
            get { return profit; }

        }
        public double Cost
        {
            get { return cost; }

        }
        double storageAmount;
        double storageMoney;
        private t_storage storage;

        public t_storage Storage
        {

            set { storage = value; }
            get { return storage; }
        }
        public StockStorageInfo(t_storage storage, int storageState, ArrayList stockList, double storageAmount, double storageMoney)
        {
            this.storage = storage;
            this.stockList = stockList;
            this.storageAmount = storageAmount;
            this.storageMoney = storageMoney;
            this.storageState = storageState;
            profit = 0;
        }
        private ArrayList stockList;

        public ArrayList StockList
        {
            get { return stockList; }
        }
        public void savaAll()
        {
            if (storage != null)
                storage.update();
            foreach (Object obj in stockList)
            {
                t_hstock stock = (t_hstock)obj;
                if (stock.saleAmount > 0.01 || stock.returnedAmount > 0.01)
                {
                    stock.update();
                }
            }
        }
        public int updateStorageForReturned(double saleAmountIn, double saleMoneyIn)
        {
            double saleAmount = saleAmountIn;
            double saleMoney = saleMoneyIn;

            // profitMoney = 0;
            profit = 0;
            if ((long)(storageAmount + 0.0001) < (long)(saleAmount))
                return 1;//忽略该项，继续结算
            storage.returnedAmount += saleAmount;
            storage.returnedMoney += saleMoney;
            storage.storageAmount = storageAmount - saleAmount;
            cost = 0;


            foreach (Object obj in stockList)
            {
                t_hstock stock = (t_hstock)obj;
                if ((long)(stock.storageAmount + 0.0001) >= (long)(saleAmount))
                {//最后一次处理
                    double storagePrice = stock.storageMoney / stock.storageAmount;//本条入库记录 库存均价计算
                    stock.storageAmount -= saleAmount;//本条入库记录库存减少
                    stock.returnedAmount += saleAmount;//本条入库记录销售数量增加
                    stock.returnedMoney += saleMoney;//本条入库记录销售金额增加

                    if ((long)(stock.storageAmount + 0.001) == 0)//本次入库是否已经销售完毕
                        stock.storageMoney = 0;
                    else
                        stock.storageMoney -= storagePrice * saleAmount;
                    cost += storagePrice * saleAmount;//本次销售 成本累加

                    if (stock.stockBoxAmount < 0)
                        stock.stockBoxAmount = 1;
                 
                    //stock.saleMoney = stock.saleMoney;
                    stock.returnedMoney = Math.Round(stock.returnedMoney, 2);
                    //    stock.update();
                    stock.lossMoney += -(saleMoney - storagePrice * saleAmount);
                    stock.lossMoney = Math.Round(stock.lossMoney, 2);

                    profit = saleMoneyIn - cost;//本次销售利润总计
                    storage.lossMoney += -profit;
                    storage.lossMoney = Math.Round(storage.lossMoney, 2);
                    storage.returnedCostMoney=Math.Round(storage.returnedCostMoney + cost,2);
                    //    profitMoney = profit;
                    //storage.saleCostMoney += cost;//总销售成本计算

                    storageAmount -= saleAmount;//入库记录合计库存数量减少
                    storageMoney -= storagePrice * saleAmount; //入库记录合计库存额减少

                    storage.storageMoney = storageMoney;//总剩余库存金额计算
                    if (storage.storageAmount > 0.001)
                        storage.storagePrice = storage.storageMoney / storage.storageAmount;
                    else
                        storage.storagePrice = 0;

                    //      storage.update();


                    //  costMoney= Math.Round(costMoney, 2);
                    return 0;//成功
                }
                else
                {//部分处理
                    if ((long)(stock.storageAmount + 0.0001) == 0)
                        continue;
                    double salePrice = saleMoney / saleAmount;//销售均价计算

                    cost += stock.storageMoney; //本条入库记录全部库存额 就是成本
                    //  costMoney += cost;
                    saleAmount -= stock.storageAmount;//本次销售 还未处理的销售量
                    saleMoney -= stock.storageAmount * salePrice;//部分处理量*销售价格就是本次处理的销售金额

                    stock.returnedMoney += stock.storageAmount * salePrice;//同理如上，本条入库记录的销售额增加
                    stock.lossMoney+=-(stock.storageAmount* salePrice- stock.storageMoney);
                    storageAmount -= stock.storageAmount;
                    storageMoney -= stock.storageMoney; //本条入库记录全部库存额 就是总库存额的减少额

                    stock.returnedAmount += stock.storageAmount;
                    //   stock.saleMoney += stock.storageAmount * salePrice;
                    stock.storageMoney = 0; //本条入库记录全部销售完毕
                    stock.storageAmount = 0;


                    if (stock.stockBoxAmount < 0)
                        stock.stockBoxAmount = 1;
                    stock.returnedMoney = Math.Round(stock.returnedMoney, 2);
                    //    stock.update();


                }
            }//for 结束

            return 2;//回滚
        }
        public int updateStorage(double saleAmountIn, double saleMoneyIn )
        {
            double saleAmount = saleAmountIn;
            double saleMoney = saleMoneyIn;

          // profitMoney = 0;
            profit = 0;
            if ((long)(storageAmount + 0.0001) < (long)(saleAmount))
                return 1;//忽略该项，继续结算
            storage.saleAmount += saleAmount;
            storage.saleMoney += saleMoney;
            storage.storageAmount = storageAmount - saleAmount;
            cost = 0;


            foreach (Object obj in stockList)
            {
                t_hstock stock = (t_hstock)obj;
                if ((long)(stock.storageAmount + 0.0001) >= (long)(saleAmount))
                {//最后一次处理
                    double storagePrice = stock.storageMoney / stock.storageAmount;//本条入库记录 库存均价计算
                    stock.storageAmount -= saleAmount;//本条入库记录库存减少
                    stock.saleAmount += saleAmount;//本条入库记录销售数量增加
                    stock.saleMoney += saleMoney;//本条入库记录销售金额增加

                    if ((long)(stock.storageAmount + 0.001) == 0)//本次入库是否已经销售完毕
                        stock.storageMoney = 0;
                    else
                        stock.storageMoney -= storagePrice * saleAmount;
                    cost += storagePrice * saleAmount;//本次销售 成本累加

                    if (stock.stockBoxAmount < 0)
                        stock.stockBoxAmount = 1;
                    stock.saleMoney = stock.saleMoney;
                    stock.saleMoney = Math.Round(stock.saleMoney, 2);
                    //    stock.update();




                    profit = saleMoneyIn - cost;//本次销售利润总计
                //    profitMoney = profit;
                    storage.saleCostMoney += cost;//总销售成本计算

                    storageAmount -= saleAmount;//入库记录合计库存数量减少
                    storageMoney -= storagePrice * saleAmount; //入库记录合计库存额减少

                    storage.storageMoney = storageMoney;//总剩余库存金额计算
                    if (storage.storageAmount > 0.001)
                        storage.storagePrice = storage.storageMoney / storage.storageAmount;
                    else
                        storage.storagePrice = 0;

                    //      storage.update();

                    
                    //  costMoney= Math.Round(costMoney, 2);
                    return 0;//成功
                }
                else
                {//部分处理
                    if ((long)(stock.storageAmount + 0.0001) == 0)
                        continue;
                    double salePrice = saleMoney / saleAmount;//销售均价计算

                    cost += stock.storageMoney; //本条入库记录全部库存额 就是成本
                    //  costMoney += cost;
                    saleAmount -= stock.storageAmount;//本次销售 还未处理的销售量
                    saleMoney -= stock.storageAmount * salePrice;//部分处理量*销售价格就是本次处理的销售金额

                    stock.saleMoney += stock.storageAmount * salePrice;//同理如上，本条入库记录的销售额增加

                    storageAmount -= stock.storageAmount;
                    storageMoney -= stock.storageMoney; //本条入库记录全部库存额 就是总库存额的减少额

                    stock.saleAmount += stock.storageAmount;
                    //   stock.saleMoney += stock.storageAmount * salePrice;
                    stock.storageMoney = 0; //本条入库记录全部销售完毕
                    stock.storageAmount = 0;


                    if (stock.stockBoxAmount < 0)
                        stock.stockBoxAmount = 1;
                    stock.saleMoney = Math.Round(stock.saleMoney, 2);
                    //    stock.update();


                }
            }//for 结束

            return 2;//回滚
        }
    }
    class SetttleDataItem
    {
        public SetttleDataItem(bool pacakgeFlag, t_commodity inCommodity, double packageAmount, t_storage storage, ArrayList stockList, double storageAmount, double storageMoney)
        {
            int storageState = inCommodity.storageState;
            this.packageFlag = pacakgeFlag;
            this.commodityID = inCommodity.commodityID;
            this.commodityName = inCommodity.commodityName;
            this.commoditySeqID = inCommodity.seqID;
            this.packageAmount = packageAmount;
            this.Unsettlecount = 0;
            this.Unsettlemoney = 0;
            storageInfo = new StockStorageInfo(storage, storageState, stockList, storageAmount, storageMoney);


        }
        private bool packageFlag;

        public bool PackageFlag
        {
            get { return packageFlag; }
        }
        private double unsettlemoney;
        private double unsettleamount;

        public double Unsettlecount
        {
            get { return unsettleamount; }
            set { unsettleamount = value; }
        }


        public double Unsettlemoney
        {
            get { return unsettlemoney; }
            set { unsettlemoney = value; }
        }
        private String commodityID;

        public String CommodityID
        {
            get { return commodityID; }
            set { commodityID = value; }
        }
        private String commodityName;

        public String CommodityName
        {
            get { return commodityName; }
            set { commodityName = value; }
        }
        private long commoditySeqID;

        public long CommoditySeqID
        {
            get { return commoditySeqID; }
            set { commoditySeqID = value; }
        }
        private double packageAmount;
        public double PackageAmount
        {
            get { return packageAmount; }
            set { packageAmount = value; }
        }



        private StockStorageInfo storageInfo;

        public StockStorageInfo StorageInfo
        {
            get { return storageInfo; }
            set { storageInfo = value; }
        }

    }
}
